package cn.com.cetccst.appstore.jssdk;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName;

import java.lang.reflect.Type;

import cn.com.cetccst.appstore.tools.Utils;
import cn.com.cetccst.sdk.modelbase.BaseResp;


//T是js请求参数的实体类  R是返回给js的实体类  CR是请求native时native返回实体类
public abstract class BaseJSAction<T, CR> {
    protected final static String TAG = "CXJSInterface";
    protected final CXApiImplInnerApp icxapi;
    protected final Context context;
    protected T para;
    protected static Gson gson;

    BaseJSAction(Context context, CXApiImplInnerApp icxapi, String parameters) {
        if (gson == null) {
            gson = new GsonBuilder()
                    .registerTypeAdapter(String[].class, new StringArrayDeserializer())
                    .create();
        }
        this.icxapi = icxapi;
        this.context = context;
        try {
            this.para = parserParameters(parameters);
        } catch (Exception e) {
            //todo response callback放在这里比较好，当异常时直接返回了。为了减少代码修改，目前转移到handleAction去处理了
        }

    }

    protected boolean checkParam(Response callback) {
        if (para == null) {
            callback.setRespJson(buildFailedResp(BaseResp.ErrCode.ERR_PARAMETER_NULL));
            return false;
        }
        return true;
    }

    public abstract void handleAction(Response callback);

    protected abstract T parserParameters(String para);

    protected abstract String buildSuccessResp(CR icxResp);

    protected String buildFailedResp(int code) {
        Resp resp = new Resp(code);
        return Utils.getGson().toJson(resp);
    }

    protected static class Resp<R> {
        @SerializedName("code")
        public int code;
        @SerializedName("msg")
        public String message;
        @SerializedName("data")
        public R data;

        public Resp() {
        }

        public Resp(int errorCode) {
            code = errorCode;
            message = "";//SDKErrorCode.getErrorMessage(code);
        }
    }

    public interface Response {
        void setRespJson(String respJson);
    }

    private static class StringArrayDeserializer implements JsonDeserializer<String[]> {
        public String[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return Utils.getGson().fromJson(json, typeOfT);
        }
    }

    protected boolean checkBaseArgs(Response callback) {
//        if (TextUtils.isEmpty(icxapi.getAppKey())) {
//            Logger.log(INFO, TAG, "parameter error!");
//            callback.setRespJson(buildFailedResp(SDKErrorCode.ERR_APP_KEY_NULL));
//            return false;
//        }
//        AppModel appModel = AppModelManager.getInstance().getApp(icxapi.getAppKey());
//        if (appModel == null) {
//            Logger.log(INFO, TAG, "appEntity is null!");
//            callback.setRespJson(buildFailedResp(SDKErrorCode.ERR_APP_NOT_ADD));
//            return false;
//        }
        return true;
    }

}
